package wechat.base.snd;

import com.google.common.base.Objects;
import java.net.URLEncoder;
import java.util.Date;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
import org.json.JSONException;
import org.json.JSONObject;
import wechat.base.snd.WCMenu;
import wechat.base.snd.WCTemplateNews;
import wechat.base.snd.util.ExecGet;
import wechat.base.snd.util.ExecPost;

@SuppressWarnings("all")
public class WCServiceApp {
  @Accessors
  private String grant_type = "client_credential";
  
  @Accessors
  private String APP_ID;
  
  @Accessors
  private String SECRET;
  
  private String authurl;
  
  private String TOKEN = null;
  
  public WCServiceApp(final String appid, final String secret) {
    this.SECRET = secret;
    this.APP_ID = appid;
    this.authurl = ((WCServiceApp.WECHAT_AUTH_URL + this.APP_ID) + "&redirect_uri=");
  }
  
  private static String WECHAT_PLATFORM_URL = "https://api.weixin.qq.com";
  
  private static String WECHAT_AUTH_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=";
  
  private static String TEMPLATE_DISPATH_MSG_ID = "v_CkyFvYjwXJq525Au4Lv71rslPW5pwcye3vPtBNlTU";
  
  private static Long TOKEN_EXPIRATION_TIME = Long.valueOf((7200 * 1000l));
  
  private static Date tokenTime = new Date();
  
  public String getToken() {
    String _xblockexpression = null;
    {
      if ((((new Date().getTime() - WCServiceApp.tokenTime.getTime()) >= (WCServiceApp.TOKEN_EXPIRATION_TIME).longValue()) || Objects.equal(this.TOKEN, null))) {
        try {
          InputOutput.<String>println("**********get token*****");
          final String tokenUrl = ((((((WCServiceApp.WECHAT_PLATFORM_URL + "/cgi-bin/token?grant_type=") + this.grant_type) + "&appid=") + this.APP_ID) + "&secret=") + this.SECRET);
          String _excuteGet = ExecGet.excuteGet(tokenUrl);
          final JSONObject js = new JSONObject(_excuteGet);
          js.getString("access_token");
          String _string = js.getString("access_token");
          this.TOKEN = _string;
          Date _date = new Date();
          WCServiceApp.tokenTime = _date;
          InputOutput.<String>println(("token=" + this.TOKEN));
        } catch (final Throwable _t) {
          if (_t instanceof Exception) {
            final Exception e = (Exception)_t;
            InputOutput.<String>println("**********Failed to get token*****");
            this.TOKEN = null;
            e.printStackTrace();
          } else {
            throw Exceptions.sneakyThrow(_t);
          }
        }
      }
      _xblockexpression = this.TOKEN;
    }
    return _xblockexpression;
  }
  
  public String getApplicationUrl() {
    String _token = this.getToken();
    return ((WCServiceApp.WECHAT_PLATFORM_URL + "/cgi-bin/message/send?access_token=") + _token);
  }
  
  public String getApplicationMediaUrl() {
    String _token = this.getToken();
    return ((WCServiceApp.WECHAT_PLATFORM_URL + "/cgi-bin/media/upload?access_token=") + _token);
  }
  
  public String getApplicationTemplateUrl() {
    String _token = this.getToken();
    return ((WCServiceApp.WECHAT_PLATFORM_URL + "/cgi-bin/message/template/send?access_token=") + _token);
  }
  
  public WCTemplateNews createNews() {
    WCTemplateNews _wCTemplateNews = new WCTemplateNews(this);
    final Procedure1<WCTemplateNews> _function = new Procedure1<WCTemplateNews>() {
      public void apply(final WCTemplateNews it) {
        it.withTempId(WCServiceApp.TEMPLATE_DISPATH_MSG_ID);
      }
    };
    return ObjectExtensions.<WCTemplateNews>operator_doubleArrow(_wCTemplateNews, _function);
  }
  
  public WCTemplateNews createTemplateNews() {
    return new WCTemplateNews(this);
  }
  
  public String buildAuthConnection(final String redirectUrl, final String state) {
    try {
      String _encode = URLEncoder.encode(redirectUrl, "utf-8");
      String _plus = (this.authurl + _encode);
      String _plus_1 = (_plus + "&response_type=code&scope=snsapi_base&state=");
      String _plus_2 = (_plus_1 + state);
      return (_plus_2 + "#wechat_redirect");
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String getOpenId(final String code) {
    try {
      String _xblockexpression = null;
      {
        final String url = (((((("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + this.APP_ID) + "&secret=") + this.SECRET) + "&code=") + code) + "&grant_type=authorization_code");
        String _excuteGet = ExecGet.excuteGet(url);
        final JSONObject jo = new JSONObject(_excuteGet);
        String _xtrycatchfinallyexpression = null;
        try {
          String _string = null;
          if (jo!=null) {
            _string=jo.getString("openid");
          }
          _xtrycatchfinallyexpression = _string;
        } catch (final Throwable _t) {
          if (_t instanceof JSONException) {
            final JSONException e = (JSONException)_t;
            Object _xblockexpression_1 = null;
            {
              e.printStackTrace();
              _xblockexpression_1 = null;
            }
            _xtrycatchfinallyexpression = ((String)_xblockexpression_1);
          } else {
            throw Exceptions.sneakyThrow(_t);
          }
        }
        _xblockexpression = _xtrycatchfinallyexpression;
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String getMenuInfo() {
    try {
      String _xblockexpression = null;
      {
        String _token = this.getToken();
        final String url = ("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + _token);
        _xblockexpression = ExecGet.excuteGet(url);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String getAllWeixinUsers() {
    try {
      String _xblockexpression = null;
      {
        String _token = this.getToken();
        final String url = ("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + _token);
        _xblockexpression = ExecGet.excuteGet(url);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String getDetailedUserInfo(final String openid) {
    try {
      String _xblockexpression = null;
      {
        String _token = this.getToken();
        String _plus = ("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + _token);
        String _plus_1 = (_plus + "&openid=");
        String _plus_2 = (_plus_1 + openid);
        final String url = (_plus_2 + "&lang=zh_CN");
        _xblockexpression = ExecGet.excuteGet(url);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String createMenu(final WCMenu menu) {
    String _token = this.getToken();
    String _plus = ("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + _token);
    JSONObject _build = menu.build();
    String _string = _build.toString();
    return ExecPost.excuteJsonPost(_plus, _string);
  }
  
  public String deleteMenu() {
    try {
      String _token = this.getToken();
      String _plus = ("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + _token);
      return ExecGet.excuteGet(_plus);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  @Pure
  public String getGrant_type() {
    return this.grant_type;
  }
  
  public void setGrant_type(final String grant_type) {
    this.grant_type = grant_type;
  }
  
  @Pure
  public String getAPP_ID() {
    return this.APP_ID;
  }
  
  public void setAPP_ID(final String APP_ID) {
    this.APP_ID = APP_ID;
  }
  
  @Pure
  public String getSECRET() {
    return this.SECRET;
  }
  
  public void setSECRET(final String SECRET) {
    this.SECRET = SECRET;
  }
}
